package insert

import (
	"fmt"
	"jsoninsert/models"
)

// InsertJson .
// aa is the json data
// 插入数据
// 保证顺序
func InsertJson(t models.Tag, aa interface{}, p int64, types int) {
	switch vv := aa.(type) {
	case string:
		// fmt.Println(k, "is string", vv)
		t.InsertTag(fmt.Sprintf("%s", vv), p, types, true)
	case int:
		// fmt.Println(k, "is int", vv)
	case float64:
		// fmt.Println(k, "is float64", vv)
	case []interface{}:
		// fmt.Println(k, "is an array:")
		for _, u := range vv {
			// t.InsertTag(fmt.Sprintf("%s", u), tempID, types, true)
			InsertJson(t, u, p, types)
		}
	case map[string]interface{}:
		// fmt.Println(k, "is an map:")
		for k, j := range vv {
			tID := t.InsertTag(fmt.Sprintf("%s", k), p, types, true)
			InsertJson(t, j, tID, types)
		}
		// InsertJson(t, v.(map[string]interface{}), tempID, types)
	default:
		fmt.Println("is of a type I don't know how to handle")
	}
}

// func InsertJson(t models.Tag, aa interface{}, p int64, types int) {
// 	m := aa.(map[string]interface{})
// 	n := aa.([]map[string]interface{})
// 	fmt.Println(n)
// 	for k, v := range m {
// 		tempID := t.InsertTag(k, p, types, true)
// 		switch vv := v.(type) {
// 		case string:
// 			fmt.Println(k, "is string", vv)
// 		case int:
// 			fmt.Println(k, "is int", vv)
// 		case float64:
// 			fmt.Println(k, "is float64", vv)
// 		case []interface{}:
// 			fmt.Println(k, "is an array:")// 			for _, u := range vv {
// 				t.InsertTag(fmt.Sprintf("%s", u), tempID, types, true)
// 			}
// 		case map[string]interface{}:
// 			fmt.Println(k, "is an map:")
// 			InsertJson(t, v.(map[string]interface{}), tempID, types)
// 		default:
// 			fmt.Println(k, "is of a type I don't know how to handle")
// 		}
// 	}
// }
